package leetcode;

public class Pro397 {
    public static void main(String[] args) {
        System.out.println(integerReplacement(7));
    }

    public static int integerReplacement(int n) {
        int[] dp = new int[n + 1];

        for (int i = 2; i <= n; i++) {
            if (i % 2 == 0) {
                dp[i] = dp[i / 2] + 1;
            } else {
                dp[i] = Math.min(dp[i - 1], dp[(i + 1) / 2] + 1) + 1;
            }
        }
        return dp[n];
    }
}
